var questionService = require('../../services/backoffice/backOffice-questionService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');


exports.backPostQuestion = function (req, res) {

    var input = req.body;

    input.community = input.community.split(',');
    input.tag = input.tag.split(',');

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('postQuestionNew - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
        } else {
            var tempImages = new Array();
            var k = 0;
            for (var i = 0; i < images.length; i++) {
                if (images[i] != null)
                    tempImages[k++] = {
                        "imageURL": images[i]
                    }
            }
            input['images'] = tempImages;

            questionService.backPostQuestionService(input, function (err, result) {
                if (err) {
                    logger.error('backPostQuestion--' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};